import {IsNotEmpty, MaxLength, ValidateIf} from 'class-validator'
import {Board as BoardModel} from "../models/Board";
import {BoardList as BoardListModel} from "../models/BoardList";
import {BoardListCard as BoardListCardModel} from "../models/BoardListCard";
import {CardAttachment as CardAttachmentModel} from "../models/CardAttachment";
import {Comment as CommentModel} from "../models/Comment";
import {Attachment as AttachmentModel} from "../models/Attachment";
import * as Boom from "boom";
import {json, where} from "sequelize";
export class PostAddBoadtBody{
    @IsNotEmpty({
        message:'面板名称不能为空'
    })
    @MaxLength(255,{
        message:'面板名称不能大于255个字符'
    })
    name:string
}
export class PutUpdateBoard {
    @ValidateIf(o=>o.name!=undefined)
    @MaxLength(255,{
        message:'面板名称不能大于255个字符'
    })
    name?:string
}
export async function getandValidateBoard(id:number,userId:number):Promise<BoardModel> {
    let board = await BoardModel.findByPk(id);
    if (!board) {
        throw Boom.notFound('指定面板不存在')

    }
    if (board.userId != userId) {
        throw Boom.forbidden('禁止访问该面板')
    }
    return board
}
export async function getlist(id:number,userId:number):Promise<any[]> {
    let list = await BoardListModel.findAll({where: {
        boardId: id
    }});
    return list
}
export async function getcards(ids:any[]):Promise<any[]> {
    let cards: any[] =[]
    console.log(ids)
    for(let i =0;i<ids.length;i++){
        let card = await BoardListCardModel.findAll({where: {
                boardListId: ids[i]
            }})
        cards = [...cards,...card]
    }
    return cards
}
export async function getcomment(ids:any[]):Promise<any[]> {
    let comments: any[] =[]

    for(let i =0;i<ids.length;i++){
        let comment = await CommentModel.findAll({where: {
                boardListCardId: ids[i]
            }})
        comments = [...comments,...comment]
    }
    return comments
}
export async function getCA(ids:any[]):Promise<any[]> {
    let cas: any[] =[]
    for(let i =0;i<ids.length;i++){
        let ca = await CardAttachmentModel.findAll({where: {
                boardListCardId: ids[i]
            }})
        cas = [...cas,...ca]
    }
    return cas
}
